#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
inline ll read() {
	ll x=0,w=1;char ch=getchar();
	for(;ch>'9'||ch<'0';ch=getchar()) if(ch=='-') w=-1;
	for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
	return x*w;
}
#define int long long
#define maxn 1000000
const double eps=1e-8;
#define	int_INF 0x3f3f3f3f
#define ll_INF 0x3f3f3f3f3f3f3f3f
int n,l,r,opt;
int get_AND(int l,int r) {
	int temp=r;int x=0;
	while(r) {
		if(l==r) {
			break;
		}
		l>>=1;r>>=1;
		x++;
	}
	return (temp>>x)<<x;
}
int get_OR(int l,int r) {
	int temp=r;int x=0;
	while(r) {
		if(l==r) {
			break;
		}
		l>>=1;r>>=1;
		x++;
	}
	return temp|(((ll)1<<x)-1);
}
int get_XOR(int l,int r) {
	l--;int x,y;
	if(l%4==1) x=1;
	else if(l%4==3) x=0;
	else if(l%4==0) x=l;
	else x=l+1;
	if(r%4==1) y=1;
	else if(r%4==3) y=0;
	else if(r%4==0) y=r;
	else y=r+1;
	return (x^y);
}
signed main() {
	n=read();
	while(n--) {
		l=read();r=read();opt=read();
		if(opt==1) {
			cout<<get_AND(l,r)<<endl;
		}else if(opt==2) {
			cout<<get_OR(l,r)<<endl;
		}else{
			cout<<get_XOR(l,r)<<endl;
		}
		
	}
	return 0;
}
/*https://blog.csdn.net/yingjiayu12/article/details/128677274*/